package com.zzyl.nursing.job;

import com.zzyl.nursing.mapper.ContractMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Component
public class ContractStatusSchedulerBatch {

    private final ContractMapper contractMapper;

    public ContractStatusSchedulerBatch(ContractMapper contractMapper) {
        this.contractMapper = contractMapper;
    }

    /**
     * 每天凌晨 1 点批量更新合同状态
     */
    // 测试每10秒执行一次
    //@Scheduled(cron = "*/10 * * * * ?")
    @Scheduled(cron = "0 0 1 * * ?")
    @Transactional
    public void batchUpdateContractStatuses() {
        LocalDateTime now = LocalDateTime.now();
        log.info("【批量合同状态更新任务】开始执行: {}", now);

        int activeCount = contractMapper.updateActiveContracts(now);
        int expiredCount = contractMapper.updateExpiredContracts(now);
        int notActiveCount = contractMapper.updateNotActiveContracts(now);

        log.info("生效中→{} 条，已过期→{} 条，未生效→{} 条", activeCount, expiredCount, notActiveCount);
        log.info("【批量合同状态更新任务】执行完毕");
    }
}
